package com.sophialex.health.record.common.ktable;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.graphics.Color;

import com.sophialex.health.common.platform.Store;
import com.sophialex.health.system.frame.resource.RcpResourceManager;
import com.sophialex.health.system.frame.widgets.dbTable.DataForMat;
import com.sophialex.health.system.frame.widgets.dbTable.IGetJoinOutValue;
import com.sophialex.health.system.frame.widgets.dbTable.JoinOutFieldDescription;

import de.kupzog.ktable.KTable;
import de.kupzog.ktable.KTableCellEditor;
import de.kupzog.ktable.KTableCellRenderer;
import de.kupzog.ktable.KTableDefaultModel;
import de.kupzog.ktable.renderers.TextCellRenderer;

/**
 * 基本KTabl的Model
 * 
 * @remark 主要封装了设置列的初始宽度，表头的名称， 加载本地存储的列宽，绑定列的值， 传递集合进来后自动赋值。
 * 
 * @author Alex
 * 
 */
public class KTableBaseModel extends KTableDefaultModel {
	protected KTable kTable;
	protected String key;
	protected String[] columnNames = new String[] { "" };
	protected String[] widths;
	protected int[] dftWidth;
	protected String[] columnProperties;
	protected List<JoinOutFieldDescription> joinOutList = new ArrayList<JoinOutFieldDescription>();
	@SuppressWarnings("unchecked")
	protected List<?> list = new ArrayList();
	protected final TextCellRenderer m_textRenderer = new TextCellRenderer(
			TextCellRenderer.INDICATION_FOCUS_ROW);
	protected int rowHeight = 24;
	public final static Color Table_Column_Color = RcpResourceManager.getColor(
			224, 239, 235);

	/**
	 * 赋值入口
	 * 
	 * @param list
	 */
	public void setInput(List<?> list) {
		this.list = list;
		kTable.redraw();
	}

	public Object getInput() {
		return list;
	}

	@Override
	public KTableCellEditor doGetCellEditor(int col, int row) {
		return new KTableCellEditTxt();
	}

	/**
	 * 设置初始的cell设置器，可重写该方法设置特效
	 */
	@Override
	public KTableCellRenderer doGetCellRenderer(int col, int row) {
		if (row < getFixedRowCount()) {
			m_textRenderer.setBackground(Table_Column_Color);
		} else {
			m_textRenderer.setBackground(null);
		}
		return m_textRenderer;
	}

	public int doGetColumnCount() {
		return columnNames == null ? 0 : columnNames.length;
	}

	/**
	 * 给具体的cell赋值。调用KTableUtil来获取值
	 */
	public Object doGetContentAt(int col, int row) {
		if (row < getFixedHeaderRowCount()) {
			return columnNames[col];
		}
		return DataForMat.toString(KTableUtil.getTableColumnText(list.get(row
				- getFixedHeaderRowCount()), this, col));
	}

	public int doGetRowCount() {
		return list.size() + getFixedRowCount();
	}

	public int getFixedHeaderRowCount() {
		return 1;
	}

	/**
	 * 获取字段属性的值的数组
	 * 
	 * @return
	 */
	public String[] getColumnProperties() {
		return columnProperties;
	}

	public void setColumnProperties(String[] columnProperties) {
		this.columnProperties = columnProperties;
	}

	/**
	 * 获取默认列宽的数组
	 * 
	 * @return
	 */
	public int[] getDftWidth() {
		return dftWidth;
	}

	public void setDftWidth(int[] dftWidth) {
		this.dftWidth = dftWidth;
	}

	public KTableBaseModel(KTable ktable, String key) {
		this.kTable = ktable;
		this.key = key;
		widths = Store.getColumnWidth(key);
	}

	public KTableBaseModel(KTable ktable, String key, String[] columnNames,
			int[] dftWidth) {
		this(ktable, key);
		this.columnNames = columnNames;
		this.dftWidth = dftWidth;
	}

	@Override
	public int getInitialColumnWidth(int column) {
		if (widths != null && column < widths.length) {
			try {
				return Integer.parseInt(widths[column]);
			} catch (NumberFormatException e) {
				return getDefaultWidth(column);
			}
		} else {
			return getDefaultWidth(column);
		}

	}

	public int getDefaultWidth(int column) {
		if (dftWidth != null && column < dftWidth.length) {
			return dftWidth[column];
		} else {
			return 100;
		}
	}

	public void saveStore() {
		Store.saveColumnWIdth(key, getColumnWidth());
	}

	public String getColumnWidth() {
		int column = getColumnCount();
		StringBuffer buffer = new StringBuffer("");
		for (int i = 0; i < column; i++) {
			if (i == 0) {
				buffer.append(getColumnWidth(i));
			} else {
				buffer.append(Store.SEPARTER).append(getColumnWidth(i));
			}
		}
		return buffer.toString();
	}

	@Override
	public void doSetContentAt(int col, int row, Object value) {

	}

	@Override
	public int getInitialRowHeight(int row) {
		return rowHeight;
	}

	public int getFixedHeaderColumnCount() {
		return 0;
	}

	public int getFixedSelectableColumnCount() {
		return 0;
	}

	public int getFixedSelectableRowCount() {
		return 0;
	}

	public int getRowHeightMinimum() {
		return 0;
	}

	public boolean isColumnResizable(int col) {
		return true;
	}

	public boolean isRowResizable(int row) {
		return true;
	}

	/**
	 * 值转换类，与DbTable通用
	 * 
	 * @return
	 */
	public List<JoinOutFieldDescription> getJoinOutList() {
		return joinOutList;
	}

	public void setJoinOutList(List<JoinOutFieldDescription> joinOutList) {
		this.joinOutList = joinOutList;
	}

	/**
	 * 设置值转换，与DbTable通用
	 * 
	 * @return
	 */
	public void setJoinOutFild(String joinFields, String byField,
			IGetJoinOutValue outJoin) {
		JoinOutFieldDescription joinOutFild = new JoinOutFieldDescription();
		joinOutFild.setJoinFields(joinFields);
		joinOutFild.setByField(byField);
		joinOutFild.setJoin(outJoin);
		joinOutList.add(joinOutFild);
	}

}
